//
//  XOFlashListViewController.m
//  didigo
//
//  Created by 海丰丛 on 2019/12/4.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOFlashListViewController.h"
#import "XOFlashListHeaderCollectionReusableView.h"
#import "EJSHorizontalGoodsCCell.h"
#import "EJSFlashSaleRequest.h"

@interface XOFlashListViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSTimer *_flashSaleTimer;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) EJSFlashSaleModel * currenFlashSale;
@property (strong, nonatomic) NSMutableArray *flashSaleList;
@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation XOFlashListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"限时抢购";
    
    _pageIndex = 1;
    [self getFlashSaleList];

    [self setupFlashSaleTimer];
    
}

- (void)configOwnViews {
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSHorizontalGoodsCCell" bundle:nil] forCellWithReuseIdentifier:@"EJSHorizontalGoodsCCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"XOFlashListHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XOFlashListHeaderCollectionReusableView"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"emptyFooterIdentifier"];
    FlyWeakSelf
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageIndex = 1;
        [weakSelf getFlashSaleList];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageIndex++;
        [weakSelf getFlashSaleList];
    }];
    weakSelf.collectionView.mj_footer.hidden = YES;

}

- (void)getFlashSaleList{
    FlyWeakSelf
    EJSFlashSaleShopRequest *req = [EJSFlashSaleShopRequest new];
    req.page = @(_pageIndex).stringValue;
    req.limit = @"20";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSFlashSaleResponseData *listModel = response.data;
        [weakSelf.collectionView.mj_header endRefreshing];
        
        if(listModel.totalPage.integerValue <= listModel.currentPage.integerValue){
            [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
        }else {
            [weakSelf.collectionView.mj_footer endRefreshing];
        }
        if(_pageIndex == 1){
            weakSelf.dataSource = [NSMutableArray arrayWithArray:listModel.list];
        }else{
            [weakSelf.dataSource addObjectsFromArray:listModel.list];
        }
        if(weakSelf.dataSource.count == 0){
            [weakSelf showNoDataViewWithText:@"没有抢购哦"];
            weakSelf.collectionView.mj_footer.hidden = YES;
        }else{
            [weakSelf hideNoDataView];
            weakSelf.collectionView.mj_footer.hidden = NO;
        }
        [weakSelf.collectionView reloadData];

    } failHandler:^(BaseResponse *response) {
        
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_footer endRefreshing];

    }];
}

#pragma mark - 计时器
- (void)setupFlashSaleTimer {
    _flashSaleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(flashSaleTimerCountDown) userInfo:nil repeats:YES];
    [_flashSaleTimer fire];
}

- (void)dealloc {
    [_flashSaleTimer invalidate];
}

- (void)flashSaleTimerCountDown{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"flashaTimerCountDownNotif" object:nil];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataSource.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EJSHorizontalGoodsCCell *horizontalGoodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSHorizontalGoodsCCell" forIndexPath:indexPath];
    EJSFlashSaleModel *saleModel = [self.dataSource objectAtIndex:indexPath.section];
    NSArray *goodsList = nil;
    goodsList = saleModel.goods;
    [horizontalGoodsCell setGoodsList:goodsList];
    return horizontalGoodsCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        XOFlashListHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"XOFlashListHeaderCollectionReusableView" forIndexPath:indexPath];
        EJSFlashSaleModel *saleModel = [self.dataSource objectAtIndex:indexPath.section];
        headerView.saleModel = saleModel;
        return headerView;
    }else{
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"emptyFooterIdentifier" forIndexPath:indexPath];
        return footerView;
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 15, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellW = (ScreenWidth-60)/4.0;
    return CGSizeMake(ScreenWidth, cellW + 46);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 121);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

@end
